package com.bmd.backgroundmanger.controller;

import com.bmd.backgroundmanger.commons.Result;
import com.bmd.backgroundmanger.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

/**
 * 首页数据统计控制器
 */
@RestController
@RequestMapping("/dashboard")
@Tag(name = "首页数据统计")
public class DashboardController {

    @Resource
    private IUserService userService;
    
    @Resource
    private IQuestionService questionService;
    
    @Resource
    private IExamPaperService examPaperService;
    
    @Resource
    private ICategoryService categoryService;

    @GetMapping("/statistics")
    @Operation(summary = "获取首页统计数据")
    public Result<Map<String, Object>> getStatistics() {
        Map<String, Object> data = new HashMap<>();
        
        try {
            // 用户总数
            long totalUsers = userService.count();
            
            // 题目总数
            long totalQuestions = questionService.count();
            
            // 试卷总数
            long totalPapers = examPaperService.count();
            
            // 分类总数
            long totalCategories = categoryService.count();
            
            // 基础统计数据
            Map<String, Object> basicStats = new HashMap<>();
            basicStats.put("totalUsers", totalUsers);
            basicStats.put("totalQuestions", totalQuestions);
            basicStats.put("totalPapers", totalPapers);
            basicStats.put("totalCategories", totalCategories);
            
            data.put("basicStats", basicStats);
            
            // 模拟一些图表数据（实际项目中应该从数据库查询）
            
            // 用户增长趋势（最近7天）
            List<Map<String, Object>> userGrowth = new ArrayList<>();
            String[] dates = {"2024-01-01", "2024-01-02", "2024-01-03", "2024-01-04", "2024-01-05", "2024-01-06", "2024-01-07"};
            int[] userCounts = {120, 132, 101, 134, 90, 230, 210};
            
            for (int i = 0; i < dates.length; i++) {
                Map<String, Object> item = new HashMap<>();
                item.put("date", dates[i]);
                item.put("count", userCounts[i]);
                userGrowth.add(item);
            }
            data.put("userGrowth", userGrowth);
            
            // 题目类型分布
            List<Map<String, Object>> questionTypes = new ArrayList<>();
            String[] types = {"单选题", "多选题", "判断题", "简答题"};
            int[] typeCounts = {45, 32, 28, 15};
            
            for (int i = 0; i < types.length; i++) {
                Map<String, Object> item = new HashMap<>();
                item.put("name", types[i]);
                item.put("value", typeCounts[i]);
                questionTypes.add(item);
            }
            data.put("questionTypes", questionTypes);
            
            // 题目难度分布
            List<Map<String, Object>> difficultyLevels = new ArrayList<>();
            String[] difficulties = {"简单", "中等", "困难"};
            int[] difficultyCounts = {60, 45, 15};
            
            for (int i = 0; i < difficulties.length; i++) {
                Map<String, Object> item = new HashMap<>();
                item.put("name", difficulties[i]);
                item.put("value", difficultyCounts[i]);
                difficultyLevels.add(item);
            }
            data.put("difficultyLevels", difficultyLevels);
            
            // 分类题目数量
            List<Map<String, Object>> categoryStats = new ArrayList<>();
            String[] categories = {"Java", "前端", "数据库", "算法", "网络"};
            int[] categoryCounts = {35, 28, 22, 20, 15};
            
            for (int i = 0; i < categories.length; i++) {
                Map<String, Object> item = new HashMap<>();
                item.put("name", categories[i]);
                item.put("value", categoryCounts[i]);
                categoryStats.add(item);
            }
            data.put("categoryStats", categoryStats);
            
            // 最近活跃数据
            List<Map<String, Object>> recentActivity = new ArrayList<>();
            String[] hours = {"00:00", "04:00", "08:00", "12:00", "16:00", "20:00"};
            int[] activities = {20, 15, 45, 80, 120, 95};
            
            for (int i = 0; i < hours.length; i++) {
                Map<String, Object> item = new HashMap<>();
                item.put("time", hours[i]);
                item.put("activity", activities[i]);
                recentActivity.add(item);
            }
            data.put("recentActivity", recentActivity);
            
            return Result.success(data);
            
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "获取统计数据失败: " + e.getMessage());
        }
    }
} 